﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Vega.USiteBuilder;
using SPCFMedicineInformer.DocumentTypes;
using SPCFMedicineInformer.Members;
using System.Web.Security;
using System.Text;
using SPCFMedicineInformer.Helpers;
namespace SPCFMedicineInformer.UserControls
{
    public partial class SignUpControl : WebUserControlBase<RegistrationPage>
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MsgLiteral.Visible = false;
            if (!IsPostBack)
            {
                LoadDates();
            }
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {                        
            if (ValidateRegistration())
            {
                string birthdate = string.Concat(ddlMonth.Items[ddlMonth.SelectedIndex].Text, " ", ddlDay.Items[ddlDay.SelectedIndex].Text, " ,", ddlYear.Items[ddlYear.SelectedIndex].Text);
                var newMember = new WebUser(Email.Text, Email.Text, Password.Text)
                {
                    Fistname = Firstname.Text,
                    Lastname = Lastname.Text,
                    Gender = rdoGender.Text,
                    Address = Address.Text,
                    Birthdate = birthdate
                };
                newMember.Save();
                if (!Roles.RoleExists(Constants.Roles.CUSTOMER))
                    Roles.CreateRole(Constants.Roles.CUSTOMER);

                Roles.AddUserToRole(newMember.LoginName, Constants.Roles.CUSTOMER);

                var sb = new StringBuilder();
                var msgFormat = "<p class=\"{0}\">{1}</p>";
                sb.AppendLine(string.Format(msgFormat, "msg done", "Successfully Registered!, Thank you"));
                MsgLiteral.Visible = true;
                MsgLiteral.Text = sb.ToString();
                clearControl(SignUpPlaceHolder);
            }
        }

        bool ValidateRegistration()
        {
           
            var sb = new StringBuilder();
            var msgFormat = "<p class=\"{0}\">{1}</p>";
            var isValidSignup = true;
            //

            if (string.IsNullOrEmpty(Firstname.Text) || string.IsNullOrEmpty(Lastname.Text))
            {
                isValidSignup = false;
                sb.AppendLine(string.Format(msgFormat, "msg warning", "Firstname & Lastname are required"));
            }
            if (string.IsNullOrEmpty(Email.Text) || string.IsNullOrEmpty(Password.Text))
            {
                isValidSignup = false;
                sb.AppendLine(string.Format(msgFormat, "msg warning", "Email & Password are required"));
            }
            else
            {
                var currentMember = MemberHelper.GetMemberFromLoginName(Email.Text);
                if (currentMember == null)                
                    currentMember = MemberHelper.GetMemberFromEmail(Email.Text);                
                if (currentMember != null)
                {
                    isValidSignup = false;
                    sb.AppendLine(string.Format(msgFormat, "msg error", "Email already Exist"));                
                }
            }
            if (Password.Text != ConfirmPassword.Text)
            {
                isValidSignup = false;
                sb.AppendLine(string.Format(msgFormat, "msg warning", "Password does not match."));
            }

            if (Email.Text != EmailConfirm.Text)
            {
                isValidSignup = false;
                sb.AppendLine(string.Format(msgFormat, "msg warning", "Email does not match."));
            }

            if (string.IsNullOrEmpty(rdoGender.Text))
            {
                isValidSignup = false;
                sb.AppendLine(string.Format(msgFormat, "msg warning", "Gender is required."));
            }

            string birthdate = string.Concat(ddlMonth.Items[ddlMonth.SelectedIndex].Text, " ", ddlDay.Items[ddlDay.SelectedIndex].Text, " ,", ddlYear.Items[ddlYear.SelectedIndex].Text);
            if (ddlDay.SelectedValue == "0" || ddlMonth.SelectedValue == "0" || ddlYear.SelectedValue == "0")
            {
                sb.AppendLine(string.Format(msgFormat, "msg warning", "Please enter your birthdate."));
                isValidSignup = false;
            }
            else if (FormHelper.GetAge(birthdate) < 0)
            {

                sb.AppendLine(string.Format(msgFormat, "msg warning", "Birthdate is not valid. Please double check."));
                isValidSignup = false;
            }


            if (!isValidSignup)
            {
                MsgLiteral.Visible = true;
                MsgLiteral.Text = sb.ToString();
            }
            return isValidSignup;
        }

        void clearControl(Control ctrl)
        {
            foreach (var _ctrl in ctrl.Controls)
            {
                if (_ctrl is TextBox)
                    ((TextBox)_ctrl).Text = string.Empty;               
            }
        }

        void LoadDates()
        {
            DateTime dteDefault = DateTime.Parse("1/1/2012");
            DataTable dtMonth = new DataTable();
            DataTable dtDay = new DataTable();
            DataTable dtYear = new DataTable();
            DataRow dr = null;

            dtMonth.Columns.Add(new DataColumn("TextField"));
            dtMonth.Columns.Add(new DataColumn("ValueField"));
            dtDay = dtMonth.Copy();
            dtYear = dtMonth.Copy();

            string textField = "TextField";
            string valueField = "ValueField";

            dr = dtMonth.NewRow();
            dr[textField] = "Month";
            dr[valueField] = "0";
            dtMonth.Rows.Add(dr);
            for (int mon = 1; mon <= 12; mon++)
            {
                dr = dtMonth.NewRow();
                dr[textField] = dteDefault.ToString("MMMM");
                dr[valueField] = mon;
                dtMonth.Rows.Add(dr);
                dteDefault = dteDefault.AddMonths(1);
            }


            dr = dtDay.NewRow();
            dr[textField] = "Day";
            dr[valueField] = "0";
            dtDay.Rows.Add(dr);
            for (int day = 1; day <= 31; day++)
            {
                dr = dtDay.NewRow();
                dr[textField] = day;
                dr[valueField] = day;
                dtDay.Rows.Add(dr);
            }

            dr = dtYear.NewRow();
            dr[textField] = "Year";
            dr[valueField] = "0";
            dtYear.Rows.Add(dr);
            for (int year = DateTime.Now.Year; year >= DateTime.Now.AddYears(-85).Year; year--)
            {
                dr = dtYear.NewRow();
                dr[textField] = year;
                dr[valueField] = year;
                dtYear.Rows.Add(dr);
            }

            FormHelper.BindDropDown(ddlDay, dtDay, textField, valueField);
            FormHelper.BindDropDown(ddlMonth, dtMonth, textField, valueField);
            FormHelper.BindDropDown(ddlYear, dtYear, textField, valueField);
        }
    }
}
